//
//  ZYAgreedDetailesController.m
//  BestLearning
//
//  Created by Ethan on 2017/9/26.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "ZYAgreedDetailesController.h"
#import "YBPopupMenu.h"

@interface ZYAgreedDetailesController ()<YBPopupMenuDelegate>
{
    IBOutlet UILabel *_title;
    IBOutlet UILabel *_youCount;
    IBOutlet UIView *_jiangLiView;
    IBOutlet UIButton *_jiangLiBtn;
    NSInteger _index;
    IBOutlet UITextField *_youNum;
    IBOutlet UIView *_otherView;
    IBOutlet UITextView *_otherTextView;
    IBOutlet UILabel *_otherViewTitle;
    IBOutlet NSLayoutConstraint *_otherTextViewRight;
    IBOutlet NSLayoutConstraint *_otherViewHeight;
    CGRect _frame;
}
@property (strong, nonatomic) NSArray *dataArr;
@end

@implementation ZYAgreedDetailesController

- (void)viewDidLoad {

    [super viewDidLoad];

    self.title = @"约定";

    _title.text = self.name;
    _youCount.text = [NSString stringWithFormat:@"当前%@个优",self.commentCount];

    _frame = _otherView.frame;
    _otherViewHeight.constant = -(_frame.size.height);
}

#pragma mark - 2017.9.16 _孔
/** 存储数组 */
- (NSArray *)dataArr {

    if (!_dataArr) {

        _dataArr = @[@"星币", @"三好套餐", @"三好蜜笈", @"三好平板", @"三好亲子游", @"其他"];
    }
    return _dataArr;
}
/** 点击了的下拉框 */
- (IBAction)ClickSelect:(UIButton *)sender {

    [self.view endEditing:YES];
    //得到控件的绝对位置
    UIWindow * window = [[[UIApplication sharedApplication] delegate] window];
    CGRect rect = [_jiangLiView convertRect:_jiangLiView.bounds toView:window];
    rect.origin.x += (rect.size.width/2);
    rect.origin.y += rect.size.height;
    // 定位弹窗
    [self ShowPopMenu:rect];
}

/** 定位弹窗,传入所传控件的绝对位置 */
- (void)ShowPopMenu:(CGRect)rect {

    if (self.dataArr.count <= 0) {

        return;
    }

    YBPopupMenu *menu = [YBPopupMenu showAtPoint:CGPointMake(rect.origin.x, rect.origin.y) titles:self.dataArr icons:nil menuWidth:100.f otherSettings:nil];
    menu.maxVisibleCount = 4;
    menu.delegate = self;
}

/** 弹窗的点击事件 */
- (void)ybPopupMenuDidSelectedAtIndex:(NSInteger)index ybPopupMenu:(YBPopupMenu *)ybPopupMenu {

    _index = index + 1;
    [_jiangLiBtn setTitle:self.dataArr[index] forState:(UIControlStateNormal)];

    if (index == 0) {

        [self ClickCoinsOrTher:YES];
    } else if (index == 5) {

        [self ClickCoinsOrTher:NO];
    } else {

        [self CloseTextField];
    }
}
#pragma mark end




- (void)ClickCoinsOrTher:(BOOL)isCoins {

    _otherTextView.text = @"";
    _otherView.hidden =  NO;
    _otherTextView.keyboardType = isCoins ? UIKeyboardTypeDecimalPad:UIKeyboardTypeDefault;

    _otherViewTitle.text = isCoins ? @"星币":@"其他";

    [self ShowTextField:isCoins];
}

- (void)CloseTextField {

    if (_otherViewHeight.constant >= 0) {

        [UIView animateWithDuration:0.3 animations:^{
            _otherViewHeight.constant = -(_frame.size.height);
            [self.view layoutIfNeeded];
        } completion:^(BOOL finished) {

            _otherView.hidden = YES;
        }];
    }
}

- (void)ShowTextField:(BOOL)isCoins {

    if (_otherViewHeight.constant <= 0 || _otherViewHeight.constant == _frame.size.height) {

        _otherView.hidden = NO;
    [UIView animateWithDuration:0.3 animations:^{
        _otherViewHeight.constant = isCoins ? 0:_frame.size.height;
        _otherTextViewRight.constant = isCoins ? 30.5:8;
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
    }];
    }
}

#pragma mark - 提交
- (IBAction)Commit:(UIButton *)sender {

    if (self.elementId.length <= 0 ||
        [NSString stringWithFormat:@"%@",self.commentId].length <= 0 ||
        _index == 0 ||
        [_youNum.text integerValue] < 1 ) {

        [SVProgressHUD showInfoWithStatus:@"请完善信息后再次约定"];
        return ;
    }

    [SVProgressHUD showWithStatus:@"请稍候"];
    NSDictionary *params = @{
                             @"action":@"saveAppoint",
                             @"type":@(_index),
                             @"parentUid":@([[AccountTool shareAccountTool].currentAccount.uid integerValue]),
                             @"content":_index == 6 ? _otherTextView.text : @"",
                             @"amount":_index == 0 ? @0:_index == 1 ? @([_otherTextView.text floatValue]):@0,
                             @"target":@([_youNum.text integerValue]),
                             @"elementId":@([self.elementId integerValue]),
                             @"commentId":@([self.commentId integerValue])
                             };
    
    [NetManager Post:LLBaseURLs(@"threeGoodElement.do") Auth:YES Params:params Success:^(id Obj) {
        if ([Obj[@"code"] isEqualToNumber:@1000]) {
            
            [SVProgressHUD showSuccessWithStatus:@"约定成功!"];
            [self performSelector:@selector(Pop) withObject:nil afterDelay:1.5];
        } else {
            
            [SVProgressHUD showErrorWithStatus:Obj[@"msg"]];
        }
        
    } Fail:^(NSError *Error) {
        //        [SVProgressHUD dismiss];
        [SVProgressHUD showErrorWithStatus:@"链接超时"];
    }];
   
}

- (void)Pop {

    [self.navigationController popViewControllerAnimated:YES];
}
@end
